package com.ccc.view;

import com.ccc.bean.Customer;
import com.ccc.service.CustomerService;
import com.ccc.util.CMUtility;

public class CustomerView {
    private static CustomerService customerService = CustomerService.getCustomerService();

    public static void main(String[] args) {
        do {
            System.out.println("-----------------客户信息管理软件-----------------");
            System.out.println("1 添 加 客 户");
            System.out.println("2 修 改 客 户");
            System.out.println("3 删 除 客 户");
            System.out.println("4 客 户 列 表");
            System.out.println("5 退      出");
            System.out.println("请选择(1-5)：_");
            char num = CMUtility.readMenuSelection();

            if (num == '1') {
                addCustomerView();
            } else if (num == '2') {
                updateCustomer();
            } else if (num == '3') {
                deleteCustomer();
            } else if (num == '4') {
                showCustomer();
            } else if (num == '5') {
                System.out.println("是否确定退出？");
                char flag = CMUtility.readConfirmSelection();
                if (flag == 'Y') break;
            }

        } while (true);
    }

    private static void showCustomer() {
        System.out.println("---------------------------客户列表---------------------------");
        System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
        //获得到所有的客户信息(数组)
        Customer[] allCustomer = customerService.getAllCustomer();
        for (int i = 0; i < allCustomer.length; i++) {
            System.out.println(allCustomer[i].getInfo());
        }
        System.out.println("-------------------------客户列表完成-------------------------");
    }

    private static void deleteCustomer() {
        System.out.println("---------------------删除客户---------------------");
        int index = -1;
        while (true) {
            System.out.print("请选择待删除客户编号(-1退出)：");
            int id = CMUtility.readInt();
            if (id == -1)
                return;
            index = customerService.getCustomerById(id);
            if (index != -1)//不等于-1说明找到，结束循环
                break;
        }
        System.out.println("确认是否删除？Y/N");
        char flag = CMUtility.readConfirmSelection();
        if (flag == 'Y') {
            boolean b = customerService.deleteCustomerByIndex(index);
            if (b)
                System.out.println("---------------------删除完成---------------------");
        }
    }

    private static void updateCustomer() {
        System.out.println("---------------------修改客户---------------------");
        int index = -1;
        while (true) {
            System.out.print("请选择待修改客户编号(-1退出)：");
            int id = CMUtility.readInt();
            //1.如果是-1
            if (id == -1)
                return;//结束当前方法
            //2. 有可能输入的编号不存在，应该是继续输入，找大管家判断
            index = customerService.getCustomerById(id);
            if (index != -1)//不等于-1说明找到，结束循环
                break;
        }
        Customer[] allCustomer = customerService.getAllCustomer();
        Customer customer = allCustomer[index];
        System.out.print("姓名(" + customer.getName() + "):");
        String name = CMUtility.readString(10, customer.getName());
        System.out.print("性别(" + customer.getGender() + "):");
        char gender = CMUtility.readChar(customer.getGender());
        System.out.print("年龄(" + customer.getAge() + ")：");
        int age = CMUtility.readInt(customer.getAge());
        System.out.print("电话(" + customer.getPhone() + ")：");
        String phone = CMUtility.readString(11, customer.getPhone());
        System.out.print("邮箱(" + customer.getEmail() + ")：");
        String email = CMUtility.readString(20, customer.getEmail());

        customer.setName(name);
        customer.setAge(age);
        customer.setGender(gender);
        customer.setPhone(phone);
        customer.setEmail(email);

        System.out.println("---------------------修改完成---------------------");
    }

    private static void addCustomerView() {
        System.out.println("--------------------添加客户---------------------");
        System.out.print("姓名：");
        String name = CMUtility.readString(10);
        System.out.print("性别：");
        char gender = CMUtility.readChar();
        System.out.print("年龄：");
        int age = CMUtility.readInt();
        System.out.print("电话：");
        String phone = CMUtility.readString(11);
        System.out.print("邮箱：");
        String email = CMUtility.readString(20);

        Customer customer = new Customer(name, gender, age, phone, email);
        boolean flag = customerService.saveCustomer(customer);
        if (flag) System.out.println("添加成功！");
        else System.out.println("添加失败！");

    }
}
